package cn.ly.core.concurrent.queue;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;

import org.junit.Test;

public class QueueTest implements Runnable{
	
	private BlockingQueue bq = null;	
	
	public QueueTest(){
	}
	
	public QueueTest(BlockingQueue bq){
		this.bq = bq;
	}
	
	@Override
	public void run() {
		String str = "";
		while(!str.equals("bye")){
			try {
				str = (String)this.bq.take();
				System.out.println("Thread read bq :" + str);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println("===run last===");
	}
	
	
	@Test
	public void testBlockQueue(BlockingQueue bq) throws IOException{
		Thread t1 = new Thread(new QueueTest(bq));
		t1.start();
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader reader = new BufferedReader(isr);
		String line = null;
		System.out.println("input :");
		while( (line = reader.readLine()) != null){
			bq.add(line);
			if(line.indexOf("bye") > -1){
				break;
			}
		}
	}
	
	
	public static void main(String[] args) throws IOException {
		new QueueTest().testBlockQueue(new PriorityBlockingQueue<String>());
	}



}
